package com.hzp.lolnews.ui.news.model;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hzp.lolnews.bean.HomeContent;
import com.hzp.lolnews.bean.HomeGrid;
import com.hzp.lolnews.bean.HomeRow;
import com.hzp.lolnews.configs.Configs;
import com.hzp.lolnews.utils.OkHttpClientUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/10/19 0019.
 */
public class INewsContentImpl implements INews {

    private int content = 1;

    @Override
    public void loadFounds(final String[] url, final OnLoadHomeListListener listener) {
        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                try {
                    //得到json字符串
                    String stringFromUrl = OkHttpClientUtils.getStringFromUrl(url[0]);
                    //传递数据
                    subscriber.onNext(stringFromUrl);
                    subscriber.onCompleted();
                } catch (IOException e) {
                    e.printStackTrace();
                    subscriber.onError(e);
                }

            }
        }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                try {
                    //接收数据并解析
                    JSONObject object = new JSONObject(s);
                    JSONObject data = object.getJSONObject("data");
                    String rows = data.getString("rows");
                    Gson gson = new Gson();
                    List<HomeRow> homeRow = gson.fromJson(rows, new TypeToken<List<HomeRow>>() {
                    }.getType());
                    listener.onSuccessHomeRow(homeRow);
                } catch (JSONException e) {
                    e.printStackTrace();
                    listener.onFailure("数据异常", e);
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                listener.onFailure("网络异常", throwable);
            }
        });

        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                try {
                    String stringFromUrl = OkHttpClientUtils.getStringFromUrl(url[0]);
                    subscriber.onNext(stringFromUrl);
                    subscriber.onCompleted();
                } catch (IOException e) {
                    e.printStackTrace();
                    subscriber.onError(e);
                }

            }
        }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                try {
                    JSONObject object = new JSONObject(s);
                    JSONObject data = object.getJSONObject("data");
                    String little_banner = data.getString("little_banner");
                    Gson gson = new Gson();
                    List<HomeGrid> homeRow = gson.fromJson(little_banner, new TypeToken<List<HomeGrid>>() {
                    }.getType());
                    listener.onSuccessHomeGrid(homeRow);
                } catch (JSONException e) {
                    e.printStackTrace();
                    listener.onFailure("数据异常", e);
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                listener.onFailure("网络异常", throwable);
            }
        });

        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                try {
                    String stringFromUrl = OkHttpClientUtils.getStringFromUrl(url[1]);
                    subscriber.onNext(stringFromUrl);
                    subscriber.onCompleted();
                } catch (IOException e) {
                    e.printStackTrace();
                    subscriber.onError(e);
                }

            }
        }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                try {
                    JSONObject object = new JSONObject(s);
                    JSONObject data = object.getJSONObject("data");
                    String rows = data.getString("rows");
                    Gson gson = new Gson();
                    List<HomeContent> contents = gson.fromJson(rows, new TypeToken<List<HomeContent>>() {
                    }.getType());
                    listener.onSuccessHomeContent(contents);
                } catch (JSONException e) {
                    e.printStackTrace();
                    listener.onFailure("数据异常", e);
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                listener.onFailure("网络异常", throwable);
            }
        });



    }

    @Override
    public void onRefresh(final String url, final OnRefreshListener listener) {

        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                try {
                    String stringFromUrl = OkHttpClientUtils.getStringFromUrl(url);
                    subscriber.onNext(stringFromUrl);
                    subscriber.onCompleted();
                } catch (IOException e) {
                    e.printStackTrace();
                    subscriber.onError(e);
                }

            }
        }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                try {
                    JSONObject object = new JSONObject(s);
                    JSONObject data = object.getJSONObject("data");
                    String rows = data.getString("rows");
                    Gson gson = new Gson();
                    List<HomeContent> contents = gson.fromJson(rows, new TypeToken<List<HomeContent>>() {
                    }.getType());
                    listener.onRefresh(contents);
                } catch (JSONException e) {
                    e.printStackTrace();
                    listener.onFailure("数据异常", e);
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                listener.onFailure("网络异常", throwable);
            }
        });

    }

    @Override
    public void onLoadMore(final String url, final OnLoadMoreListener listener) {
        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                try {
                    String format = String.format(Configs.urlMore, content++);
                    String stringFromUrl = OkHttpClientUtils.getStringFromUrl(format);
                    subscriber.onNext(stringFromUrl);
                    subscriber.onCompleted();
                } catch (IOException e) {
                    e.printStackTrace();
                    subscriber.onError(e);
                }

            }
        }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                try {
                    JSONObject object = new JSONObject(s);
                    JSONObject data = object.getJSONObject("data");
                    String rows = data.getString("rows");
                    Gson gson = new Gson();
                    List<HomeContent> contents = gson.fromJson(rows, new TypeToken<List<HomeContent>>() {
                    }.getType());
                    listener.onLoadMore(contents);
                } catch (JSONException e) {
                    e.printStackTrace();
                    listener.onFailure("数据异常", e);
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                listener.onFailure("网络异常", throwable);
            }
        });

    }
}
